package com.LachesismStorm.lachesism.core.util;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author lachesism
 * @Date 2023-05-23 21:59
 **/

@Service
public class Blacklist {
    private static Map<String, LocalDateTime> blacklist = new ConcurrentHashMap<>();

    public static void addToBlacklist(String token) {
        LocalDateTime expirationTime = LocalDateTime.now().plusSeconds(60*60*48);
        blacklist.put(token, expirationTime);
    }
    public static boolean isBlacklisted(String token) {
        //如果里面存在了
        return blacklist.containsKey(token);
    }
    @Scheduled(fixedDelay = 1000*60*60*24)
    public void clearExpiredItems() {
        LocalDateTime now = LocalDateTime.now();
        blacklist.entrySet().removeIf(entry -> entry.getValue().isBefore(now));
    }

}